/*
 * @lc app=leetcode.cn id=1419 lang=cpp
 *
 * [1419] 数青蛙
 */
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <cmath>

using namespace std;
// @lc code=start
class Solution {
public:
    //检查字符串是否合法
    bool checkValid(string croakOfFrogs) {
        int cNum = 0, rNum = 0, oNum = 0, aNum = 0, kNum = 0;
        for (auto val : croakOfFrogs) {
            if (val == 'c') ++cNum;
            else if (val == 'r') ++rNum;
            else if (val == 'o') ++oNum;
            else if (val == 'a') ++aNum;
            else if (val == 'k') ++kNum;
            if (rNum > cNum || oNum > rNum || aNum > oNum || kNum > aNum)
                return false;
        }
        return cNum == rNum && cNum == oNum && cNum == aNum && cNum == kNum;
    }

    int minNumberOfFrogs(string croakOfFrogs) {
        int n = croakOfFrogs.length();
        int ret = 0;
        int croaking = 0;
        for(auto val : croakOfFrogs) {
            if(val == 'c') {
                croaking++;
                ret = max(ret,croaking);
            }
            if(val == 'k') {
                croaking--;
            }
        }
        return checkValid(croakOfFrogs)? ret : -1; 
    }
};
// @lc code=end

